﻿#region USING STATEMENTS
using System;
using System.Threading;
using Fusion.Management;
using Microsoft.Xna.Framework;
#endregion

#region CODE BODY
namespace Fusion.States
{
    public class LoadState : BaseState
    {
        //////////////////////////////////////////////////
        // Data Fields
        //////////////////////////////////////////////////
        private Thread LoadingThread;
        private BaseState nextState;
        private Boolean Waiting;

        private Single DotTimer;
        private String LoadText = "Loading";

        //////////////////////////////////////////////////
        // Member Properties
        //////////////////////////////////////////////////
        public BaseState NextState
        {
            get { return nextState; }
            set { nextState = value; }
        }

        //////////////////////////////////////////////////
        // Constructors
        //////////////////////////////////////////////////
        public LoadState()
        {
        }

        //////////////////////////////////////////////////
        // Menu Methods
        //////////////////////////////////////////////////
        public override void Enter()
        {
            Waiting = true;
            LoadingThread = new Thread(NextState.Enter);
            LoadingThread.Start();
        }
        public override void Exit()
        {
            if (LoadingThread.IsAlive)
                LoadingThread.Abort();
            LoadingThread = null;
        }
        public override Boolean Input(Single Delta)
        {
            return true;
        }
        public override void Update(Single Delta)
        {
            if (Waiting)
            {
                DotTimer += Delta;

                if (DotTimer > 0.5f)
                {
                    LoadText += '.';
                    DotTimer = 0f;
                }

                if (!LoadingThread.IsAlive)
                    Waiting = false;
                return;
            }
            else
            {
                GC.Collect();
                FStates.PopState();
            }
        }
        public override void Render(float Delta)
        {
            FCanvas.Begin();
            FCanvas.DrawString(LoadText, new Vector2(10f, 680f), Color.White);
            FCanvas.End();
        }
    }
}
#endregion

//////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2012 Jeffery Thomas Sventora
//////////////////////////////////////////////////////////////////////////////////////////////